package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public PageResult<Category> queryCategoryListWithPage(Integer currentPage, Integer pageSize) {
        // 分页
        PageHelper.startPage(currentPage, pageSize);
        // 封装树形Category
        Category category = new Category();
        category.setId(0);
        Category condition = new Category();
//        List<Category> categoryList = categoryMapper.select(category);
//
//        if (CollectionUtils.isEmpty(categoryList)) {
//            throw new LyException(ExceptionEnum.Category_NOT_FOUND);
//        }
//
//        for (Category c2 : categoryList) {
//            category.setPid(c2.getId());
//            c2.setChildren(categoryMapper.select(category));
//            for (Category c3 : c2.getChildren()) {
//                category.setPid(c3.getId());
//                c3.setChildren(categoryMapper.select(category));
//            }
//        }

        queryWithLevel(category, 3);
        List<Category> categoryList = category.getChildren();
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.Category_NOT_FOUND);
        }
        PageInfo<Category> info = new PageInfo<>(categoryList);


        return new PageResult<>((int) info.getTotal(), categoryList);
    }

    public void queryWithLevel(Category category, int level) {
        Category condition = new Category();
        condition.setPid(category.getId());
        List<Category> list = categoryMapper.select(condition);
        if (!CollectionUtils.isEmpty(list)) {
            category.setChildren(list);
            if (list.get(0).getLevel() != level) {

                for (Category cate : category.getChildren()) {
                    queryWithLevel(cate, level);
                }
            }
        }
    }

    public void addCategory(Category category) {
        int i = categoryMapper.insert(category);
        if (i == 1) {
          throw new LyException(ExceptionEnum.CATEGORY_ADD_FAILED);
        }
    }

    public void updateCategory(Category category) {
        int i = categoryMapper.updateByPrimaryKey(category);
        if (i == 1) {
            throw new LyException(ExceptionEnum.CATEGORY_UPDATE_FAILED);
        }
    }

    public void deleteCategory(int id, int level) {
        Category category = new Category();
        category.setId(id);
        category.setLevel(level);
        queryWithLevel(category, level);
        deleteRecursive(category);
    }

    public void deleteRecursive(Category category) {
        List<Category> children = category.getChildren();
        if (!CollectionUtils.isEmpty(children)) {
            for (Category cate : children) {
                deleteRecursive(cate);
            }
        }
        categoryMapper.deleteByPrimaryKey(category.getId());
    }

    public void queryWithAllLevel(Category category) {
        List<Category> categoryList = categoryMapper.selectAll();
        queryLevelData(category, categoryList);
    }

    private void queryLevelData(Category category, List<Category> categoryList) {
        if (categoryList != null &&  categoryList.size() > 0) {
            List<Category> children = new ArrayList<>();
            Iterator<Category> iterator = categoryList.iterator();
            while (iterator.hasNext()) {
                Category next = iterator.next();
                if (next.getPid().equals(category.getId())) {
                    children.add(next);
                    iterator.remove();
                }
            }
            if (children.size() > 0) {
                category.setChildren(children);
                for (Category child: children) {
                    queryLevelData(child, categoryList);
                }

            }
        }
    }

}
